package cn.thr.ymcc.web.controller;

import cn.thr.ymcc.CourseDoc;
import cn.thr.ymcc.dto.OrderDto;
import cn.thr.ymcc.jwt.LoginContextUtils;
import cn.thr.ymcc.query.CourseCartQuery;
import cn.thr.ymcc.result.JsonResult;
import cn.thr.ymcc.result.PageList;
import cn.thr.ymcc.service.ICourseCartService;
import cn.thr.ymcc.domain.CourseCart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/courseCart")
public class CourseCartController {

    @Autowired
    public ICourseCartService courseCartService;

    @Autowired
    private LoginContextUtils loginContextUtils;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 购物车结算
     * @param courseIds
     * @return
     */
    @PostMapping("/calAmount")
    public JsonResult calAmount(@RequestBody List<Long> courseIds,HttpServletRequest request){
        return JsonResult.me(courseCartService.calAmount(courseIds,request));
    }

    /**
     * 购物车查询数据
     * @param request
     * @return
     */
    @GetMapping("/listForUser")
    public JsonResult listForUser(HttpServletRequest request){
        Long loginUserId = loginContextUtils.getLoginUserId(request.getHeader(LoginContextUtils.JWT_TOKEN_NAME));
        return JsonResult.me(courseCartService.list(new QueryWrapper<CourseCart>().eq("login_id",loginUserId)));
    }

    /**
     * 幂等性校验 ：生成token 存储到redis ，每次业务执行前线判断redis 中是否在这个key
     * 存在：执行业务，后删除这个key
     * 不存在就不执行这个业务
     * @return
     */
    @GetMapping("/createToken")
    public JsonResult createToken(){
        String token = UUID.randomUUID().toString();

        //保存到redis 里面去
        redisTemplate.opsForValue().set(token,1,10, TimeUnit.MINUTES);

        return  JsonResult.me(token);
    }

    @PostMapping("/add")
    public JsonResult placeOrder(@RequestBody CourseDoc courseDoc, HttpServletRequest request){
        return courseCartService.add(courseDoc,request);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody CourseCart courseCart){
        if(courseCart.getId()!=null){
            courseCartService.updateById(courseCart);
        }else{
            courseCartService.save(courseCart);
        }
        return JsonResult.me();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseCartService.removeById(id);
        return JsonResult.me();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.me().setData(courseCartService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.me().setData(courseCartService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseCartQuery query){
        Page<CourseCart> page = new Page<CourseCart>(query.getPageNo(), query.getPageSize());
        page = courseCartService.page(page);
        return JsonResult.me().setData(new PageList<CourseCart>(page.getTotal(), page.getRecords()));
    }
}
